﻿package mortal.game.view.copy.defenseCopy.view
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.copy.defenseCopy.*;
    import mortal.mvc.core.*;

    public class DefenseCopyUnfoldGoodsCellRenderer extends GCellRenderer
    {
        private var _name:GTextFiled;
        private var _baseItem:BaseItem;
        private var _price:TextBox;
        private var _itemdata:ItemData;

        public function DefenseCopyUnfoldGoodsCellRenderer()
        {
            return;
        }// end function

        private function get itemData() : TDefenseCopyShopSell
        {
            return _data as TDefenseCopyShopSell;
        }// end function

        private function onClick(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyComposeUpdate, this.itemData.itemCode));
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            this._itemdata = new ItemData(this.itemData.itemCode);
            this._baseItem.itemData = this._itemdata;
            this._name.htmlText = HTMLUtil.addColor(this._itemdata.name, ColorConfig.instance.getItemColor(this._itemdata.itemInfo.color).color);
            this.updateMoney();
            if (this.itemData.sellType == EDefenseCopyShopSellType._EDefenseCopyShopSellScroll)
            {
                NetDispatcher.addCmdListener(ServerCommand.BackPackItemAdd, this.onBagUpdate);
                this.onBagUpdate();
            }
            else
            {
                this._baseItem.amount = 0;
                this.filters = [];
                NetDispatcher.removeCmdListener(ServerCommand.BackPackItemAdd, this.onBagUpdate);
            }
            return;
        }// end function

        private function onBagUpdate(param1:Object = null) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Array = null;
            var _loc_4:SDefenseCopyBuyItem = null;
            var _loc_5:int = 0;
            if (this.itemData)
            {
                _loc_2 = 0;
                _loc_3 = Cache.instance.copy.defenseCopy.copyShopBuyItems;
                _loc_5 = 0;
                while (_loc_5 < _loc_3.length)
                {
                    
                    _loc_4 = _loc_3[_loc_5];
                    if (_loc_4.itemCode == this.itemData.itemCode)
                    {
                        _loc_2 = _loc_4.amount;
                        break;
                    }
                    _loc_5++;
                }
                this._baseItem.amount = this.itemData.maxBuyAmount - _loc_2;
                this.filters = this._baseItem.amount <= 0 ? ([FilterConst.colorFilter2]) : ([]);
            }
            return;
        }// end function

        private function updateMoney(param1:Object = null) : void
        {
            if (this.itemData == null || Cache.instance.copy.defenseCopy.copyReward == null)
            {
                return;
            }
            var _loc_2:* = DefenseCopyUtil.getPriceByCode(this.itemData.itemCode);
            var _loc_3:* = Cache.instance.copy.defenseCopy.copyReward.equipPoint;
            var _loc_4:* = _loc_3 >= _loc_2 ? (GlobalStyle.colorBai) : (GlobalStyle.colorHong);
            this._price.htmlText = HTMLUtil.addColor(_loc_2.toString(), _loc_4);
            return;
        }// end function

        private function onCopyComposeUpdate(event:DataEvent) : void
        {
            this._baseItem.filters = event.data == this.itemData.itemCode ? ([FilterConst.itemChooseFilter]) : (null);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._name = UIFactory.gTextField("", 0, 8, 80, 20, this, GlobalStyle.textFormatPutong.center());
            this._baseItem = UIFactory.baseItem((80 - 42) / 2, 34, 42, 42, this);
            this._baseItem.setItemStyle(36, ImagesConst.PetItemBg, 3, 3);
            this._baseItem.configEventListener(MouseEvent.CLICK, this.onClick);
            this._price = UIFactory.getUICompoment(TextBox, 20, 80, this);
            this._price.bgHeight = 22;
            this._price.bgWidth = 50;
            this._price.textFieldWidth = 50;
            this._price.textFieldHeight = 22;
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.Jinbi, 0, 80, this));
            Dispatcher.addEventListener(EventName.DefenseCopyComposeUpdate, this.onCopyComposeUpdate);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyRewardUpdate, this.updateMoney);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyBagUpdate, this.updateMoney);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyRoleEquipAdd, this.updateMoney);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            Dispatcher.removeEventListener(EventName.DefenseCopyComposeUpdate, this.onCopyComposeUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemAdd, this.onBagUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyRewardUpdate, this.updateMoney);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyBagUpdate, this.updateMoney);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyRoleEquipAdd, this.updateMoney);
            this._baseItem.dispose(param1);
            this._name.dispose(param1);
            this._price.dispose(param1);
            this._baseItem = null;
            this._name = null;
            this._price = null;
            return;
        }// end function

    }
}
